package CreationalPattern.Prototype.DeepClone;

import lombok.Data;

import java.io.*;

@Data
public class WeeklyLog implements Serializable {
    private Attachment attachment;
    private String name;
    private String date;
    private String content;

    public WeeklyLog clone() {
        try{
            // 写入流中
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(this);

            // 从流中拿出对象
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            return (WeeklyLog)objectInputStream.readObject();
        } catch (Exception e) {
            System.out.println("复制失败");
            return null;
        }
    }
}
